package com.easyadmin.pro.common.initutils;

import com.alibaba.fastjson.JSONObject;
import com.easyadmin.pro.common.constants.SystemConstants;
import com.easyadmin.pro.common.vo.RouterTree;
import com.easyadmin.pro.modules.sys.service.IRouterService;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * packageName com.easyadmin.pro.common.initUtils
 *
 * @author 骑着蚂蚁去上天
 * @version JDK 17
 * @className InitRouterTree
 * @date 2024/4/16
 * @description 初始化路由树
 */
@Component
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class InitRouterTree {

    private final IRouterService routerService;

    private final RedisTemplate redisTemplate;

    @PostConstruct
    public void init() {
        List<RouterTree> routerTree = routerService.initRouterTree(0L);
        redisTemplate.opsForValue().set(SystemConstants.ROUTER_KEY, JSONObject.toJSONString(routerTree));
    }
}